import { _decorator, Component, Node } from 'cc';
import { http_get, http_patch, http_post, HttpResponse } from '../../sgame/http/Http';
const { ccclass, property } = _decorator;
import GameHttp, { gamehttp_get } from '../../sgame/http/GameHttp';
import { llog, lwarn } from '../../sgame/logger/Logger';

@ccclass('Main')
export class Main extends Component {
    private baseURL: string = "https://echo.apifox.com";

    start() {
        this.testGet();
    }

    testGet() {
        lwarn("test get");
        http_get(this.baseURL + "/get", {
            name: "test",
            age: 18
        }, (response: HttpResponse) => {
            llog("response type", typeof response.data);
            llog("response", response.data);
            this.testPost();
        }, {
            headers: {
                "Test-Header": "what?"
            }
        });
    }

    testPost() {
        lwarn("test post");
        http_post(this.baseURL + '/post', {
            name: "test",
            age: 18
        }, (response: HttpResponse) => {
            llog("response type", typeof response.data);
            llog("response", response.data);
            this.testJsonPost();
        })
    }

    testJsonPost() {
        lwarn("test json post");
        http_post(this.baseURL + '/post', {
            name: "test",
            age: 18
        }, (response: HttpResponse) => {
            llog("response type", typeof response.data);
            llog("response", response.data);
            this.testPatch();
        }, {
            headers: {
                "Content-Type": "application/json",
                "Test-Header": "what?"
            }
        });
    }

    testPatch() {
        lwarn("test patch");
        http_patch(this.baseURL + '/patch', {
            name: "test",
            age: 18
        }, (response: HttpResponse) => {
            llog("response type", typeof response.data);
            llog("response", response.data);
        }, {
            headers: {
                "Content-Type": "application/json",
            }
        })
    }

    testGameGet() {
        gamehttp_get(this.baseURL + "/get.php", {
            name: "test",
            age: 18
        }, (response: HttpResponse) => {
            console.log(response.data, typeof response.data);
        }, {
            responseType: "json",
            timeout: 5000,
            headers: {
                "Content-Type": "application/json",
                "SELF": "NO SELF"
            },
            // noCallback: true
        });
    }
}